package com.ebupt.migu.fineoperationmedia.controller;

import com.ebupt.migu.common.pojo.Topic;
import com.ebupt.migu.fineoperationmedia.entity.ApiQueryBean;
import com.ebupt.migu.fineoperationmedia.entity.response.ResponseBean;
import com.ebupt.migu.fineoperationmedia.service.ApiQueryService;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Optional;

/**
 * @author liuyangyang@e-byte.com
 * @description: TODO
 * @date 2024/4/16 15:26
 */
@RestController
@RequestMapping("/api")
public class MediaController {
    @Autowired
    private ApiQueryService service;

    @RequestMapping("/demo")
    public HashMap<String, Object> demo(@RequestBody ApiQueryBean apiQueryBean) {
        HashMap<String, Object> res = new HashMap<>();
        res.put("code", "0000");
        res.put("message", "success");
        res.put("data", apiQueryBean);
        return res;
    }

    @RequestMapping("/query")
    public ResponseBean query(@Validated @RequestBody ApiQueryBean bean) {
        // 同步外部系统的requestId替换自己生成的
        Optional.ofNullable(bean.getExtra().get(Topic.REQUEST_ID_KEY)).ifPresent(e -> MDC.put(Topic.REQUEST_ID_KEY, e));
        return service.query(bean);
    }
}
